import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { Events } from '../entity/Events';
import { Soldier } from '../entity/Soldier';
import { Utils } from '../common/Utils';
import { TrainEvent } from '../entity/TrainEvent';
import { EventOper } from '../entity/Buildings';
import { UIManager } from '../common/UIManager';
import VillageApi from '../api/VillageApi';
import { UserInfo } from '../common/UserInfo';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('SoldierTrainItem')
export class SoldierTrainItem extends Component {
    @property(Sprite)
    cover:Sprite
    @property(Label)
    soldierName:Label
    @property(Label)
    count:Label
    @property(Label)
    time:Label
    @property(Node)
    speed:Node

    trainEvent:TrainEvent

    start() {
        this.speed.on(Node.EventType.TOUCH_END, this.onSpeed, this)
    }

    async setData(trainEvent:TrainEvent){
        this.trainEvent = trainEvent
        
        this.soldierName.string = trainEvent.soldierName
      
        this.count.string = trainEvent.soldierNum + ''

        this.time.string = Utils.formatDate(new Date(trainEvent.totalEndTime * 1000) , 'yyyy-MM-dd\n hh:mm:ss')
        let soldier = Soldier.createById(trainEvent.soldierType)
        this.cover.spriteFrame = await soldier.getHeadImg()
    }

    async onSpeed(){
       

        let param = {
            ac: EventOper.SPEED_BUILD,
            eid: this.trainEvent.eHandle
        }

        await UIManager.instance.showLoading()
        let res = await VillageApi.eventOper(param)
        UIManager.instance.closeLoading()
        console.log(`加速升级，ac =${param.ac} ,del =${param.eid} `, res)

        await UserInfo.instance.refreshVillageDetail()

        EventManager.instance.emit(EventManager.EventType.TrainSoldier,1)
    }
}


